package com.paginasenlanube.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Random;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.paginasenlanube.model.Website;

public class Utils {
	

	

	public static JsonArray getJsonArray(String jsonString) {
		try {
			jsonString = URLDecoder.decode(jsonString, "UTF-8");
		
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		JsonParser parser = new JsonParser();
		JsonElement tradeElement = parser.parse(jsonString);
		JsonArray json = tradeElement.getAsJsonArray();
		return json;
	}
	
	public static String getJsonVar(JsonArray json,String var){
		String value="";
		JsonElement element = json.get(0).getAsJsonObject().get(var);
		if(element != null){
			value = element.getAsString();
		}
		return value;
	}
	
	public static String getImagePrefix(){
		  Random rdm = new Random();
	      String pre = String.valueOf(rdm.nextInt(999999999));
	      return pre;
	}
	
	public static Map<String,Integer> getImageDimension(String key,String pageId,Website w,Properties myProps){
		Map<String,Integer> dimensions = new HashMap<String,Integer>();
		
		if(key.equals("design")){
			dimensions.put("width", Integer.parseInt( myProps.getProperty(w.getDesign().getTemplate()+"_width").trim()));
			dimensions.put("height", Integer.parseInt( myProps.getProperty(w.getDesign().getTemplate()+"_height").trim()));
		 }
		if(key.equals("header")){
			dimensions.put("width", Integer.parseInt(myProps.getProperty("logo_width").trim()));
			dimensions.put("height", Integer.parseInt(myProps.getProperty("logo_height").trim()));
		 }
		if(key.equals("header_gal")){
			dimensions.put("width", Integer.parseInt( myProps.getProperty(w.getDesign().getTemplate()+"_header_width").trim()));
			dimensions.put("height", Integer.parseInt( myProps.getProperty(w.getDesign().getTemplate()+"_header_height").trim()));
		 }
		if(key.equals("dataType") || key.equals("archive") || key.equals("gallery")){
			long id = Long.parseLong(pageId);
			String pageType = w.getMenu().getPage(id).getPageType();
			String template = w.getMenu().getPage(id).getTemplate();
			dimensions.put("width", Integer.parseInt( myProps.getProperty(pageType+"."+template+"_width").trim()));
			dimensions.put("height", Integer.parseInt( myProps.getProperty(pageType+"."+template+"_height").trim()));
		 }
		
		return dimensions;
	}

}
